package com.pdf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.itextpdf.awt.geom.Rectangle;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.AcroFields.Item;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;

public class TestPdf {

	public static void test1() throws MalformedURLException, IOException, DocumentException{
		 Rectangle pSize=new Rectangle(144,90);
	        //文档的背景色
	       // pSize.set.setBackgroundColor(Color.blue);             
	        //创建一个文档对象，设置初始化大小和页边距
	        Document pdfDoc=new Document(PageSize.A4,5,5,5,5);
		FileOutputStream pdfFile = new FileOutputStream(new File("C:/Users/KK/Downloads/firstPdf.pdf"));
		Paragraph paragraph = new Paragraph("My first PDF file with an image ...");
		Image image = Image.getInstance("C:/Users/KK/Downloads/cross.png");
		PdfWriter.getInstance(pdfDoc, pdfFile);
		pdfDoc.open(); // 打开 Document 文档
		pdfDoc.add(paragraph);
		pdfDoc.add(image);
		pdfDoc.addTitle("第一个pdf文件");
		pdfDoc.addSubject("测试");
		pdfDoc.add(new Paragraph("HelloWorld", FontFactory.getFont(FontFactory.COURIER, 20, Font.BOLD, new BaseColor(255, 0, 0))));
		PdfPTable t = new PdfPTable(2);
    /* t.setBorderColor(new Color(0, 0, 255));//设置边框颜色为蓝色
     t.setPadding(5);
     t.setSpacing(5);//设置单元格边界
     t.setBorderWidth(1);//设置边框宽度
		Cell c1 = new Cell("header1");
     c1.setHeader(true);
     t.addCell(c1);
     c1 = new Cell("Header2");
     t.addCell(c1);
     t.endHeaders();
     t.addCell("1.1");
     t.addCell("1.2");
     doc.add(t);
		pdfDoc.close();
     */
	}
	public static void addField() {
		try {
			Document document=new Document(PageSize.A4);
			FileOutputStream os=new FileOutputStream("E:/PDF/template.pdf");
			PdfWriter pw = PdfWriter.getInstance(document, os);
			document.open();
			document.newPage();
			
			PdfFormField pff=new PdfFormField(pw, 40, 40, 80, 40, null);
			PdfAnnotation pff2=PdfFormField.createText(pw, new com.itextpdf.text.Rectangle(40, 40, 80, 40), "abc", "cc", true, null);
			pff2.setName("itext1");	
			
			pw.addAnnotation(pff2);
			pff.setName("itext2");
			pff.setFieldName("itextField2");
			//TextField
			
			document.addTitle("First PDF");
			document.setPageCount(1);
			//pw.close();
			document.close();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public static void setField() {
		try {
			Document document=new Document(PageSize.A4);
			FileOutputStream os=new FileOutputStream("E:/PDF/template2.pdf");
			PdfReader reader = new PdfReader("E:/PDF/template.pdf");
			PdfWriter pw=PdfWriter.getInstance(document, os);
			document.open();
			PdfStamper ps =new PdfStamper( reader, os);
			AcroFields af=ps.getAcroFields();
			System.out.println("itext="+af.getField("itext"));
			System.out.println("itextField2="+af.getField("itextField2"));
			System.out.println("itext2="+af.getField("itext2"));
			af.setField("text1", "hello");
			ps.close();
			document.close();			
			pw.close();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	/**
	 * @param args
	 * @throws IOException 
	 * @throws MalformedURLException 
	 * @throws DocumentException 
	 */
	public static void main(String[] args) throws MalformedURLException, IOException, DocumentException {
		addField();
		setField();
	}
}
